package com.fzrskj.cloud.free.sys.dao;

import java.util.List;
import java.util.Map;

import com.fzrskj.cloud.free.sys.bean.SysAgentRelationBean;
import ms.core.consts.SysErr;
import ms.core.db.DbUtilsDrv;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

public class SysAgentRelationDao extends CObject{
	/**
	 * private declarations.
	 */
	private IDbDrv db=null;

	/**
	 * constructor function to initialize database access object.
	 */
	public SysAgentRelationDao(){ db=new DbUtilsDrv(); }

	/**
	 * to free resource.
	 */
	@Override
	public void dispose(){
		if (db!=null) {
			db.closeConnection();
		}
		db = null;
		super.dispose();
	}

	/**
	 * new record to database by map
	 * @param values
	 * @return
	 */
	public int newSysAgentRelation(Map<String, Object> values){
		int ret = db.insertRecord("sys_agent_relation",values);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		
		this.setErrCode(err);
		return ret;
	}

	/**
	 * to delete table record by id.
	 * @param id
	 * @return
	 */
	public int delSysAgentRelation(int id){
		String sql="delete from sys_agent_relation where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		
		this.setErrCode(err);
		return ret;
	}

	/**
	 * to delete table record by cdt.
	 * @param cdt
	 * @return
	 */
	public int delSysAgentRelation(Map<String, Object> cdt){
		int ret = db.deleteRecord("sys_agent_relation",cdt);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		
		this.setErrCode(err);
		return ret;
	}

	/**
	 * to modify table record by values and condition.
	 * @param values
	 * @param cdt
	 * @return
	 */
	public int modSysAgentRelation(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("sys_agent_relation", values, cdt);
		String err = db.getErrCode();
		this.setErrCode(err);
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	/**
	 * to query sys_agent_relation item for detail.
	 * @param id
	 * @return
	 */
	public SysAgentRelationBean querySysAgentRelation(int id){
		String sql="select * from sys_agent_relation where id=?";
		SysAgentRelationBean ret = db.queryForBean(sql,SysAgentRelationBean.class,new Object[]{id});
		String err=db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(err);
		return ret;
	}

	/**
	 * to query SysAgentRelation record count.
	 * @param cdt
	 * @return
	 */
	public int querySysAgentRelation(Map<String, Object> cdt){
		int ret = db.queryRecordCount("sys_agent_relation", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	public SysAgentRelationBean queryOneSysAgentRelation (Map<String, Object> cdt) {
		List<SysAgentRelationBean> infoList = this.querySysAgentRelation(cdt, "", 1, 1);
		if (null == infoList || infoList.isEmpty()) {
			return null;
		}
		return infoList.get(0);
	}

	/**
	 * to query SysAgentRelationBean for record list with condition,if page or pagesize less than 0, means without page.
	 * @param cdt
	 * @param orderby
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<SysAgentRelationBean> querySysAgentRelation(Map<String,Object> cdt,String orderby,int page,int pagesize){
		List<SysAgentRelationBean> rets = db.queryForBeans("sys_agent_relation","*",orderby,SysAgentRelationBean.class,cdt,page,pagesize);
		String err=db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		
		this.setErrCode(err);
		return rets;
	}
}